package cn.com.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.po.IctCommoditiesQuery;
import com.cabinet.service.ILatticeService;
import com.cabinet.service.IMCommoditiesService;
import com.cabinet.web.entity.Cabinet;
import com.cabinet.web.entity.Diauthority;
import com.cabinet.web.entity.Lattice;

import cn.com.cabinet.dao.CabinetDao;
import cn.com.cabinet.dao.DiauthorityDao;

import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/diauth")
public class DiauthorityController {
	
	@Autowired
	private DiauthorityDao diauthorityDao;
	@Autowired
	private IMCommoditiesService iMCommoditiesService;
	
	@Autowired
	private ILatticeService iLatticeService;
	
	@Autowired
	private CabinetDao cabinetDao;
	
	@RequestMapping("/chcek")
	@ResponseBody
	public String diaCheck(ModelMap ModelMap,HttpServletRequest request) throws Exception{
		if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
			return "请选择要补货的智能柜";
		}else{
			String cid = CookieUtils.getCookieValue(request, "cid");
			String userid = CookieUtils.getCookieValue(request, "userid");
			Diauthority d = diauthorityDao.getDauthorityByUserId(Long.valueOf(userid));
			if(d != null){
				Cabinet c = cabinetDao.getCabinetByCidAndSellerId(cid, d.getSellerId());
				if(c == null){
					return "您的权限不够，无法管理此智能柜！";
				}else{
					return "1";
				}
			}else{
				return "您还不是补货员，不要调皮哟";
			}
		}
	}
	
	@RequestMapping("/index")
	public String Index(ModelMap modelMap,HttpServletRequest request) throws NumberFormatException, Exception{
		if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
			return "redirect:/user/detail";
		}else{
			String cid = CookieUtils.getCookieValue(request, "cid");
			String userid = CookieUtils.getCookieValue(request, "userid");
			Diauthority d = diauthorityDao.getDauthorityByUserId(Long.valueOf(userid));
			if(d != null){
				Cabinet c = cabinetDao.getCabinetByCidAndSellerId(cid, d.getSellerId());
				if(c == null){
					return "redirect:/user/detail";
				}else{
					
					IctCommoditiesQuery ictCommoditiesQuery = new IctCommoditiesQuery();
					ictCommoditiesQuery.setCabId(cid);
					List<IctCommoditiesQuery> ictCommoditiesQueryList = iMCommoditiesService.queryCommoditiesByCidAndClaName(ictCommoditiesQuery);
					modelMap.addAttribute("ictCommoditiesQueryList", ictCommoditiesQueryList);
					return "/diauth/diauth";
				}
			}else{
				return "redirect:/user/detail";
			}
		}
	}
	
	
	@RequestMapping("/update")
	@ResponseBody
	public String update(ModelMap modelMap,HttpServletRequest request,Long Latid) throws NumberFormatException, Exception{
		if(CookieUtils.getCookieValue(request, "cid") == null || CookieUtils.getCookieValue(request, "cid").equals("null")){
			return "权限不足";
		}else{
			String cid = CookieUtils.getCookieValue(request, "cid");
			String userid = CookieUtils.getCookieValue(request, "userid");
			Diauthority d = diauthorityDao.getDauthorityByUserId(Long.valueOf(userid));
			if(d != null){
				Cabinet c = cabinetDao.getCabinetByCidAndSellerId(cid, d.getSellerId());
				if(c == null){
					return "权限不足";
				}else{
					
					Lattice la = iLatticeService.getLattice(Latid);
					la.setLaNum(1);
					iLatticeService.updateLattice(la);
					return "更新成功";
				}
			}else{
				return "权限不足";
			}
		}
	}

}
